/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FontSettingPanel.java
 *
 * Created on Nov 3, 2010, 8:59:58 AM
 */
package dae.fxcreator.ui.usersettings;

import dae.fxcreator.io.FXSettings;
import dae.fxcreator.io.FXSingleton;
import dae.fxcreator.node.gui.GraphFont;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.event.ItemEvent;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import javax.swing.ListModel;

/**
 *
 * @author Koen
 */
public class FontSettingPanel extends javax.swing.JPanel {

    /** Creates new form FontSettingPanel */
    public FontSettingPanel() {
        initComponents();
        String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        lstFontNames.setListData(fontNames);

        FXSettings settings = FXSingleton.getSingleton().getFXSettings();
        lstFonts.setModel(settings.getAllFonts());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        scrFonts = new javax.swing.JScrollPane();
        lstFonts = new javax.swing.JList();
        btnNewFont = new javax.swing.JButton();
        lblFontFamily = new javax.swing.JLabel();
        scrFontFamily = new javax.swing.JScrollPane();
        lstFontNames = new javax.swing.JList();
        scrFontSizes = new javax.swing.JScrollPane();
        lstFontSizes = new javax.swing.JList();
        lblFontSize = new javax.swing.JLabel();
        cboBold = new javax.swing.JCheckBox();
        cboItalic = new javax.swing.JCheckBox();
        lblFontNames = new javax.swing.JLabel();
        lblColor = new javax.swing.JLabel();
        lblColorValue = new javax.swing.JLabel();
        btnChangeColor = new javax.swing.JButton();
        lblPreview = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(746, 400));
        setLayout(new java.awt.GridBagLayout());

        lstFonts.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstFonts.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstFontsValueChanged(evt);
            }
        });
        scrFonts.setViewportView(lstFonts);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 0, 5);
        add(scrFonts, gridBagConstraints);

        btnNewFont.setText("New Font ...");
        btnNewFont.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewFontActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 5, 5);
        add(btnNewFont, gridBagConstraints);

        lblFontFamily.setText("Font family : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 0);
        add(lblFontFamily, gridBagConstraints);

        lstFontNames.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstFontNames.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstFontNamesValueChanged(evt);
            }
        });
        scrFontFamily.setViewportView(lstFontNames);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 0, 5);
        add(scrFontFamily, gridBagConstraints);

        lstFontSizes.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "32", " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstFontSizes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstFontSizesValueChanged(evt);
            }
        });
        scrFontSizes.setViewportView(lstFontSizes);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 5, 0, 5);
        add(scrFontSizes, gridBagConstraints);

        lblFontSize.setText("Font size : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 0);
        add(lblFontSize, gridBagConstraints);

        cboBold.setText("Bold");
        cboBold.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboBoldItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 0, 0);
        add(cboBold, gridBagConstraints);

        cboItalic.setText("Italic");
        cboItalic.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboItalicItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 0, 0);
        add(cboItalic, gridBagConstraints);

        lblFontNames.setText("Font names :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 0);
        add(lblFontNames, gridBagConstraints);

        lblColor.setText("Color : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 0, 0);
        add(lblColor, gridBagConstraints);

        lblColorValue.setBackground(new java.awt.Color(0, 0, 0));
        lblColorValue.setMaximumSize(new java.awt.Dimension(50, 20));
        lblColorValue.setMinimumSize(new java.awt.Dimension(50, 20));
        lblColorValue.setOpaque(true);
        lblColorValue.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 0, 0);
        add(lblColorValue, gridBagConstraints);

        btnChangeColor.setText("...");
        btnChangeColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeColorActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 0, 0);
        add(btnChangeColor, gridBagConstraints);

        lblPreview.setText("The quick brown fox jumps over the lazy dog");
        lblPreview.setBorder(javax.swing.BorderFactory.createTitledBorder("Preview"));
        lblPreview.setMaximumSize(new java.awt.Dimension(269, 100));
        lblPreview.setMinimumSize(new java.awt.Dimension(100, 100));
        lblPreview.setPreferredSize(new java.awt.Dimension(400, 100));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 10;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(lblPreview, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void lstFontsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstFontsValueChanged
        // TODO add your handling code here:
        GraphFont font = (GraphFont) lstFonts.getSelectedValue();
        if ( font == null)
            return;
        lstFontNames.setSelectedValue(font.getFont().getName(), true);
        lstFontSizes.setSelectedValue(Integer.toString(font.getFont().getSize()), true);
        cboBold.setSelected(font.getFont().isBold());
        cboItalic.setSelected(font.getFont().isItalic());

        Color fontColor = font.getColor();
        
        lblColorValue.setBackground(fontColor);

        lblPreview.setFont(font.getFont());
        lblPreview.setForeground(fontColor);

    }//GEN-LAST:event_lstFontsValueChanged

    private void lstFontNamesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstFontNamesValueChanged
        GraphFont font = (GraphFont) lstFonts.getSelectedValue();
        if (font == null)return;
        font.setFontFamily(lstFontNames.getSelectedValue().toString());

        lblPreview.setFont(font.getFont());
    }//GEN-LAST:event_lstFontNamesValueChanged

    private void cboBoldItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboBoldItemStateChanged
        GraphFont font = (GraphFont) lstFonts.getSelectedValue();
        if (font == null)return;
        font.setBold(evt.getStateChange() == ItemEvent.SELECTED);
        lblPreview.setFont(font.getFont());
    }//GEN-LAST:event_cboBoldItemStateChanged

    private void cboItalicItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboItalicItemStateChanged
        GraphFont font = (GraphFont) lstFonts.getSelectedValue();
        if (font == null)return;
        font.setBold(evt.getStateChange() == ItemEvent.SELECTED);
        lblPreview.setFont(font.getFont());
    }//GEN-LAST:event_cboItalicItemStateChanged

    private void lstFontSizesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstFontSizesValueChanged
        GraphFont font = (GraphFont) lstFonts.getSelectedValue();
        if (font == null)return;
        font.setFontSize(Integer.parseInt(lstFontSizes.getSelectedValue().toString()));
        lblPreview.setFont(font.getFont());
    }//GEN-LAST:event_lstFontSizesValueChanged

    private void btnChangeColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeColorActionPerformed
        GraphFont font = (GraphFont) lstFonts.getSelectedValue();
        if (font == null)return;
        Color selectedColor = JColorChooser.showDialog(this, TOOL_TIP_TEXT_KEY, font.getColor());
        if ( selectedColor != null){
            font.setFontColor(selectedColor);
            lblColorValue.setBackground(selectedColor);
        }
        lblPreview.setForeground(font.getColor());
    }//GEN-LAST:event_btnChangeColorActionPerformed

    private void btnNewFontActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewFontActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:
        String fontName = JOptionPane.showInputDialog(this, "Create new style", "Font settings", JOptionPane.QUESTION_MESSAGE);
        if (fontName != null && fontName.length() > 0) {
            FXSingleton singleton = FXSingleton.getSingleton();
            FXSettings settings = singleton.getFXSettings();
            if (!settings.hasFont(fontName)) {
                GraphFont gf = new GraphFont(fontName,"Courier",12,false,false,false,Color.black);
                settings.addFont(fontName, gf);
                lstFonts.setModel(settings.getAllFonts());
                ListModel lm = lstFonts.getModel();
                for (int i = 0; i < lm.getSize(); ++i)
                {
                    GraphFont element = (GraphFont)lm.getElementAt(i);
                    if (element == gf)
                    {
                        lstFonts.setSelectedIndex(i);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "Font allready exists", "Font settings", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnNewFontActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChangeColor;
    private javax.swing.JButton btnNewFont;
    private javax.swing.JCheckBox cboBold;
    private javax.swing.JCheckBox cboItalic;
    private javax.swing.JLabel lblColor;
    private javax.swing.JLabel lblColorValue;
    private javax.swing.JLabel lblFontFamily;
    private javax.swing.JLabel lblFontNames;
    private javax.swing.JLabel lblFontSize;
    private javax.swing.JLabel lblPreview;
    private javax.swing.JList lstFontNames;
    private javax.swing.JList lstFontSizes;
    private javax.swing.JList lstFonts;
    private javax.swing.JScrollPane scrFontFamily;
    private javax.swing.JScrollPane scrFontSizes;
    private javax.swing.JScrollPane scrFonts;
    // End of variables declaration//GEN-END:variables
}
